home *** CD-ROM | disk | FTP | other *** search
- Path: news.logicon.com!newsmaster@klee
- From: kkolda@logicon.com (Kenneth D. Kolda)
- Newsgroups: comp.lang.c++
- Subject: Re: Using the %c control in SCANF
- Date: 8 Apr 1996 21:11:44 GMT
- Organization: Logicon Operating Systems
- Message-ID: <4kbveg$6jh@piper.logicon.com>
- References: <4k9g0e$eom@knot.queensu.ca>
- NNTP-Posting-Host: 137.51.122.161
- Mime-Version: 1.0
- X-Newsreader: WinVN 0.99.2
-
- In article <4k9g0e$eom@knot.queensu.ca>, 3mb42@qlink.queensu.ca says...
- >
- >Can anyone please give me some examples of using the %c control in the
- >scanf function.
- >
- >I keep getting segmentation faults when I use %c in the following line:
- >
- > scanf ("%s%c", textLine);
- >
- >the variable "textLine" is declared as
- >
- > char textLine[MAXBUFFERSIZE];
- >
- >while MAXBUFFERSIZE is an integer constant that equals 256.
- >
- >I am attempting to receive input, but not only up to the next non-blank
- >character. I want all of what is typed until the return key is hit.
- >
- >Can anyone help me use the %c control properly? If not, do you know of a
- >better way of accomplishing this, perhaps with iostream.h functions
- instead?
- >
- >Any help is appreciated, thanks.
- >
- >M
- >
-
- The segmentation fault is because you have two scan codes, %s and %c, but
- only one object to write to (textLine).
-
- A nice way to do this is using:
-
- cin.getline(textLine, MAXBUFFERSIZE);
-
- This will read all input from stdin until a newline (up to a max of
- MAXBUFFERSIZE -- a safety catch that your scanf does not have). The only
- thing to watch out for is that there are no newlines sitting in the stdin
- buffer that haven't been read -- your cin.getline() will return having
- read 0 characters.
-
- Ken Kolda
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-